home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRIPT.PAK / OPENFILE.SPP < prev    next >
Text File  |  1997-05-06  |  6KB  |  210 lines

  1. //----------------------------------------------------------------------------
  2. // cScript
  3. // (C) Copyright 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. // OPENFILE.SPP
  6. //    Opens a file.
  7. //
  8. // $Revision:   1.27  $
  9. //
  10. //----------------------------------------------------------------------------
  11.  
  12. import editor;
  13. import IDE;
  14.  
  15. // mark this module as being a library module
  16. library;
  17.  
  18. //
  19. // This function is tied to the Editor's local menu command "Open Source"
  20. // There should be a file name at/near the current cursor location.  Fetch it
  21. // into an edit buffer.
  22. //
  23. LoadFileAtCursor(){
  24.  
  25.    declare quotedFlag = false;
  26.    declare bracketedFlag = false;
  27.  
  28.    declare ep = editor.TopView.Position;
  29.  
  30.    // Get the token delimited by whitespace or the whole line...
  31.    declare bFoundDelimiter = false;
  32.  
  33.    // Will we need to contend with a quote or angle bracket?
  34.    ep.Save();
  35.  
  36.    declare nCurrentLine = ep.Row;
  37.  
  38.    if (ep.Search("[<\"]",false,true,SEARCH_BACKWARD,BRIEF_RE)) {
  39.       if (ep.Row == nCurrentLine) {
  40.          bFoundDelimiter = ep.Character;
  41.       } else {
  42.          ep.Restore();
  43.          ep.Save();
  44.       }
  45.    }
  46.  
  47.    //  Use whitespace as a delimiter
  48.    if (!bFoundDelimiter)
  49.       ep.MoveCursor(SKIP_NONWHITE|SKIP_LEFT);
  50.  
  51.    declare String sLine(ep.Read());
  52.    ep.Restore();
  53.  
  54.    // Parse out file name.
  55.  
  56.    // Look for angle brackets or quotes.
  57.    declare nEndPos = 0;
  58.    declare nStartPos = sLine.Index("<");
  59.  
  60.    if (!nStartPos) { 
  61.       // We didn't have an angle bracket
  62.       nStartPos = sLine.Index("\"");
  63.       if (!nStartPos) { 
  64.          // We didn't have a quote. Start from the begining of the token.
  65.          nStartPos = 0; 
  66.       } else {  
  67.          // Find the other quote. Strip out first quote.
  68.          quotedFlag = true;
  69.          sLine = sLine.SubString(nStartPos);
  70.          nStartPos = 0;
  71.          nEndPos = sLine.Index("\"") - 1;
  72.       }
  73.    } else { 
  74.       // Try to find the matching angle bracket. Strip out opening bracket.
  75.       sLine = sLine.SubString(nStartPos);
  76.       nStartPos = 0;
  77.       nEndPos = sLine.Index(">") - 1;
  78.       bracketedFlag = true;
  79.    }
  80.  
  81.    if (!nEndPos) {  
  82.        // Look for whitespace delimeter
  83.        nEndPos = sLine.Index(" ");
  84.        if (!nEndPos)
  85.           nEndPos = sLine.Index("\t");
  86.        if (!nEndPos)
  87.           nEndPos = sLine.Index("\n");
  88.    }
  89.  
  90.    sLine = sLine.SubString(nStartPos,nEndPos);
  91.  
  92.    sLine = sLine.Trim();
  93.    sLine = sLine.Trim(TRUE);
  94.  
  95.    if (sLine.Length) {
  96.  
  97.       // The new ANSI style header notation allows system header files to be 
  98.       // specified without extension.  As in:
  99.       //   #include <stdio>
  100.       // if we detect an extensionless header file in angle brackets we'll 
  101.       // append a .h so we can find them.
  102.       if(bracketedFlag){
  103.          if(sLine.Index(".") == 0){
  104.             sLine = new String(sLine.Text + ".h");
  105.          }
  106.       }
  107.  
  108.       SmartFileLoad(sLine.Text, editor.TopBuffer.FullName, quotedFlag);
  109.       return;
  110.    }
  111.  
  112.   IDE.FileOpen("");
  113. }
  114.  
  115.  
  116. // Search the specified path for the file
  117. FindFileOnPath(fileName, path){
  118.  
  119.    if(fileName == NULL || fileName == "")
  120.       return NULL;
  121.  
  122.    if(path == NULL || path == "")
  123.       return NULL;
  124.  
  125.    declare String fullPath(path);
  126.    declare String matchPath;
  127.    declare endPos = 0;
  128.    do{
  129.       endPos = fullPath.Index(";");
  130.       if(endPos){
  131.          matchPath = fullPath.SubString(0, endPos - 1);
  132.          fullPath = fullPath.SubString(endPos);
  133.       }else{
  134.          matchPath = fullPath;
  135.       }
  136.  
  137.       declare checkThis = matchPath.Text + "\\" + fileName;
  138.       if(IDE.FileExists(checkThis)){
  139.          return checkThis;
  140.       }
  141.  
  142.    }while(endPos);
  143.  
  144.    return NULL;
  145. }
  146.  
  147. SmartFileLoad(declare fileToLoad, declare origFile, declare bQuotedFlag){
  148.    declare foundName = NULL;
  149.  
  150.    // first, see if we can find a matching node in the current project
  151.    declare prjNode = new ProjectNode(fileToLoad);
  152.    if(prjNode.IsValid){
  153.       foundName = prjNode.InputName;
  154.    }
  155.  
  156.    // second, look in the current directory first if filename enclosed in quotes
  157.    if (bQuotedFlag) {
  158.       if(foundName == NULL){
  159.          foundName = FindFileOnPath(fileToLoad, ".");
  160.       }
  161.    }
  162.  
  163.    // Let's use the associated editor to obtain the proper project node.
  164.    declare ProjectNode srcFile(NULL,editor.TopView);
  165.  
  166.    // Well if it isn't an associated view then... We look using the file name.
  167.    if(!srcFile.IsValid){
  168.       // find a project node to use as a base
  169.       srcFile = new ProjectNode(origFile);
  170.    }
  171.  
  172.    // The name didn't work so we will use a general purpose node object.
  173.    if(!srcFile.IsValid){
  174.       srcFile = new ProjectNode();
  175.    }
  176.  
  177.    // third, see if we can find it along the include path
  178.    if(foundName == NULL){
  179.       foundName = FindFileOnPath(fileToLoad, srcFile.IncludePath);
  180.    }
  181.  
  182.    // fourth, try the source path
  183.    if(foundName == NULL){
  184.       foundName = FindFileOnPath(fileToLoad, srcFile.SourcePath);
  185.    }
  186.  
  187.    // last chance, look in the current directory if we haven't already
  188.    if (!bQuotedFlag) {
  189.       if(foundName == NULL){
  190.          foundName = FindFileOnPath(fileToLoad, ".");
  191.       }
  192.    }
  193.  
  194.    if(foundName == NULL){
  195.       // still can't find it - see if user knows
  196.       if(IDE.UseCurrentWindowForSourceTracking){
  197.          editor.NewView(fileToLoad, true);
  198.       }else{
  199.          IDE.FileOpen(fileToLoad);
  200.       }
  201.    }else{
  202.       // open it
  203.       if(IDE.UseCurrentWindowForSourceTracking){
  204.          editor.NewView(foundName);
  205.       }else{
  206.          IDE.DoFileOpen(foundName);
  207.       }
  208.    }
  209. }
  210.